<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<!--bootstrap-fileinput-->

	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css">
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
<!--                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>-->
<!--                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>-->
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>
									      <th class="sorting">是否有效</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
<!--			                          <tr>-->
<!--			                              <td><input  type="checkbox"></td>			                              -->
<!--				                          <td>1</td>-->
<!--									      <td>1</td>-->
<!--									      <td>促销海报1</td>-->
<!--									      <td>http://wwww.hb.com/hd1.html</td>-->
<!--									      <td>-->
<!--									      	<img alt="" src="" width="100px" height="50px">-->
<!--									      </td>-->
<!--									      <td>1</td>-->
<!--									      <td>有效</td>									     								     -->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="categoryId">
							<option value="-1">请选择</option>
							<option th:each="cc : ${cc}" th:value="${cc.id}" th:text="${cc.name}"></option>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="title">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="url">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="sortOrder">  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<label for="uimg">上传图片</label>
									<input type="file" class="form-control" name="myFile" id="uimg">
									<input type="text" name="pic" id="pic">
								</td>
<!--								<td>-->
<!--									<img  src="{{entity.pic}}" width="200px" height="100px">-->
<!--								</td>-->
							</tr>
							<tr>
								<td>是否有效</td>
								<td>
									<input type="checkbox" class="icheckbox_square-blue"  id="status">
								</td>
							</tr>
						</table>
		      		</td>
		      	</tr>
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insertContent()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <input id="id">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="category">
								  <option th:each="cc : ${cc}" th:value="${cc.id}" th:text="${cc.name}"></option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  class="form-control" placeholder="标题" id="title1">  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  class="form-control" placeholder="URL" id="url1">  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  class="form-control" placeholder="排序" id="sortOrder1">  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <label for="uimg">上传图片</label>
										  <input type="file" class="form-control" name="myFile" id="uimg1">
										  <input type="text" name="pic" id="pic1">
									  </td>
									  <td id="img"></td>
									  <!--								<td>-->
									  <!--									<img  src="{{entity.pic}}" width="200px" height="100px">-->
									  <!--								</td>-->
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" class="icheckbox_square-blue"  id="statuss">
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>

</html>
<script>

	$(function(){
		getData();
		fileInit();
		fileInit1();
		$("categoryId").select2();
		$("category").select2();
	})

	function getData(){
		$.ajax({
			url:"/contentController/getContent",
			type:"post",
			dataType:"json",
			data:{},
			success:function (result){
				var data = result.data;
				var table = "";
				for (var i=0;i < data.length;i++){
					table += '<tr><td><input  type="checkbox" value="'+ data[i].id+'"></td><td>'+data[i].id+'</td><td>'+data[i].categoryId+'</td>';
					table += '<td>'+data[i].title+'</td><td>'+data[i].url+'</td>';
					table += '<td><img alt="" src="'+data[i].pic+'" width="100px" height="50px"></td>';
					table += '<td>'+data[i].sortOrder+'</td>';

						if(data[i].status == "1"){
							table += '<td>有效</td>';
						}else {
							table += '<td>无效</td>';
						}

					table += '<td class="text-center"><button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal1" onclick="toUpdate('+ data[i].id+')">修改</button></td></tr>';
				}
				$("#tbody").html(table);
			},
			error:function (){
				alert("查询错误")
			}
		})
	}

	//增加图片
	function fileInit(){
		$('#uimg').fileinput({
			language: 'zh',
			uploadUrl: '/contentController/upLoadFile',
			maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
			enctype : 'multipart/form-data',
			maxFileSize : 1024, //单位为kb
			allowedFileExtensions : ['jpg', 'gif', 'png'],
			browseClass: 'btn btn-primary'
		});

		$('#uimg').on('fileuploaded',function(event, data, previewId, index){
			if(data.response){//如果有返回值 那么就最终结果就是true
				//   var imgUrl = data.response.imgUrl;//原先上传图片
				var imgUrl = data.response.data;//阿里云上传 setData(url)在后段方法里把url设置给了data，所以想拿到url就得.data
				$("#pic").val(imgUrl);
			}
		})
	}

	function insertContent(){
		var category = $("#categoryId").select2("data");
		var categoryId = category.val();
		var title = $("#title").val();
		var url = $("#url").val();
		var pic = $("#pic").val();
		var status = "";
		if ($("#status").is(":checked")){
			status = "1"
		}else {
			status = "0"
		}
		var sortOrder = $("#sortOrder").val();
		$.ajax({
			url:"/contentController/insertContent",
			type:"post",
			dataType:"json",
			data:{categoryId:categoryId,title:title,url:url,sortOrder:sortOrder,pic:pic,status:status},
			success:function (result){
				alert("增加成功");
				location.reload();
			},
			error:function (){
				alert("增加错误")
			}
		})
	}

	//回显图片
	function fileInit1(){
		$('#uimg1').fileinput({
			language: 'zh',
			uploadUrl: '/contentController/upLoadFile',
			maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
			enctype : 'multipart/form-data',
			maxFileSize : 1024, //单位为kb
			allowedFileExtensions : ['jpg', 'gif', 'png'],
			browseClass: 'btn btn-primary'
		});

		$('#uimg1').on('fileuploaded',function(event, data, previewId, index){
			if(data.response){//如果有返回值 那么就最终结果就是true
				//   var imgUrl = data.response.imgUrl;//原先上传图片
				var imgUrl = data.response.data;//阿里云上传 setData(url)在后段方法里把url设置给了data，所以想拿到url就得.data
				$("#pic1").val(imgUrl);
			}
		})
	}


	function toUpdate(id){
		$.ajax({
			url:"/contentController/toUpdate/"+id,
			type:"post",
			dataType:"json",
			data:{},
			success:function (result){
				var data = result.data;
				$("#id").val(data.id);
				$("#category").val(data.categoryId).trigger("change");
				$("#title1").val(data.title);
				$("#url1").val(data.url);
				// $("#pic1").val(data.pic);
				var tbody="";//图片回显
				tbody +="<td><img src='"+data.pic+"' width='200px' height='150px'><input type='hidden' value='"+data.pic+"' id='imgg'></td>";
				$("#img").html(tbody);
				$("#sortOrder1").val(data.sortOrder);
				if (data.status == 1){
					$("#statuss").attr("checked",true);
				}else if (data.status == 0){
					$("#statuss").attr("checked",false);
				}

			},
			error:function(){
				alert("回显错误");
			}
		})
	}

	function updateInfo(){
		var id = $("#id").val();
		var category = $("#categoryId").select2("data");
		var categoryId = category.id;
		var title = $("#title1").val();
		var url = $("#url1").val();
		var pic = "";
		if ($("#pic1").val() != null && $("#pic1").val() !=''){
			pic =$("#pic1").val();
		}else {
			pic =$("#imgg").val();
		}
		var status = "";
		if ($("#statuss").is(":checked")){
			status = "1"
		}else {
			status = "0"
		}
		var sortOrder = $("#sortOrder").val();
		$.ajax({
			url:"/contentController/updateContent",
			type:"post",
			dataType:"json",
			data:{id:id,categoryId:categoryId,title:title,url:url,sortOrder:sortOrder,pic:pic,status:status},
			success:function (result){
				alert("修改成功");
				location.reload();
			},
			error:function (){
				alert("修改错误")
			}
		})
	}

	function deleteBatch(){
		var boxs=$("[type=checkbox]:checked");
		var ids=[];
		$(boxs).each(function (i,e) {
			ids.push(e.value);
		})
		if (ids.length<=0){
			return;
		}

		$.ajax({
			url:"/contentController/deleteContent",
			type:"post",
			dataType:"json",
			data:{ids:ids},
			success:function (result) {
				alert("删除成功");
				window.location.reload();
			},
			error:function () {
				alert("删除失败")
			}
		})
	}
</script>